﻿using System;
using System.Collections.Generic;
using Android.Content;
using Android.Views;
using Android.Widget;

namespace Cinemacenter.Droid
{
	class DrawerAdapter : ArrayAdapter<string>
	{
		readonly Context context;
		readonly IList<string> values;
		ViewHolderInfo holderInfo;

		public DrawerAdapter (Context context, IList<string> values) : base (context, Resource.Layout.ViewDrawerOptItem, values)
		{
			this.context = context;
			this.values = values;
		}

		public override View GetView (int position, View convertView, ViewGroup parent)
		{
			if (convertView == null) {
				var inflater = LayoutInflater.FromContext (context);
				convertView = inflater.Inflate (Resource.Layout.ViewDrawerOptItem, parent, false);
				holderInfo = new ViewHolderInfo ();
				convertView.SetTag (Resource.Id.drawerItemHolder, holderInfo);
				holderInfo.Icon = convertView.FindViewById<ImageView> (Resource.Id.iconView);
				holderInfo.ItemText = convertView.FindViewById<TextView> (Resource.Id.itemText);
			} else {
				holderInfo = (ViewHolderInfo)convertView.GetTag (Resource.Id.drawerItemHolder);
			}

			var item = values [position];
			holderInfo.ItemText.Text = item;

			var imageId = context.Resources.GetIdentifier ("com.ldb.marquecine:drawable/ic_opt" + position, null, null);
			holderInfo.Icon.SetImageResource (imageId);
			return convertView;
		}
	}

	public class ViewHolderInfo : Java.Lang.Object
	{
		public ImageView Icon;
		public TextView ItemText;
	}
}
